import allure
import jsonpath
import logging

from utils.send_request_utils import send_sql_request

@allure.step("核心步骤3：http断言")
def asserts_http(case,res):

    expected = eval(case["expected"])
    # check有值时,实际结果是否等于预期结果
    if case["check"]:
        #多个断言，用列表装的，把字符串变成列表
        check = eval(case["check"])
        for c , e in zip(check, expected):
            result = jsonpath.jsonpath(res.json(), c)[0]
            logging.info(f"第3个: http断言中，将判断预期结果({e}) == 实际结果({result})？")
            allure.attach(f"预期结果({e}) == 实际结果({result})？", "http断言数据")
            assert result == e

    # check为空，判断是否包含即可,text为文本格式
    else:
        for e in case["expected"]:
            logging.info(f"第3个: http断言中，将判断预期结果({e}) 是否in 实际结果({res.text})中？")
            allure.attach(f"预期结果({e}) in 实际结果({res.text})？", "http断言数据")
            assert str(e) in res.text

def asserts_jdbc(case,res):
    # 当两个都有值时
    if case["sql_check"] and case["sql_expected"]:
        with allure.step("核心步骤3：jdbc断言"):
            result = send_sql_request(case["sql_check"])
            logging.info(f"第3个: jdbc断言中，将判断预期结果({case['sql_expected']}) == 实际结果({result})？")
            allure.attach(f"预期结果({case['sql_expected']}) == 实际结果({result})？", "jdbc断言数据")
            assert result == case["sql_expected"]